package com.sofwin.service.impl;/**
 * @PackageName:com.sofwin.service.impl
 * @author:andyLiu
 * @DateTime:2020/12/25 15:45
 * @Description:[TODO]输入当前模块作用
 **/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.mapper.SRoleMapper;
import com.sofwin.pojo.SRole;
import com.sofwin.pojo.SRoleExample;
import com.sofwin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RoleServiceImpl
 * @Description: TODO
 * @Author: andyliu
 * @Date: 2020/12/25 15:45
 * @Version: 1.0
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SRoleMapper mapper;
    @Override
    public List<SRole> queryRoles() {
        return mapper.selectByExample(null);
    }

    @Override
    public PageInfo<SRole> queryRoleByPage(SRole role, Integer pageNumber) {
        SRoleExample ex = new SRoleExample();
        SRoleExample.Criteria criteria = ex.createCriteria();
        if(role!=null && role.getRoleName()!=null && !"".equals(role.getRoleName())){
            criteria.andRoleNameLike("%"+role.getRoleName()+"%");
        }
        PageHelper.startPage(pageNumber, Constants.PAGE_SIZE);
        List<SRole> roles = mapper.selectByExample(ex);
        return new PageInfo<SRole>(roles);
    }

    @Override
    public Map saveRole(SRole role) {
        int i = 0;
        Map map = new HashMap();
        if (role.getId() != null){
            i = mapper.updateByPrimaryKeySelective(role);

        }else {
            i = mapper.insert(role);
        }
        if (i == 1){
            map.put("sta",1);
            map.put("msg","添加成功");
        }else {
            map.put("sta",0);
            map.put("msg","添加失败");
        };
        return map;
    }

    @Override
    public SRole selectOneById(SRole role) {
        return mapper.selectByPrimaryKey(role.getId());
    }

    @Override
    public boolean deleteRoleById(Integer[] ids) {
        boolean flag=false;
        int count=0;
        for (Integer id:ids) {
            count+= mapper.deleteByPrimaryKey(id);
        }
        if (count==ids.length){
            flag=true;
        }
        return flag;
    }
}
